﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MemCached;
using System.Data;
using DataBaseAccessLib;
using BusinessModel.EntityModel;
using System.Reflection;

namespace BusinessModel
{
    public class MaterialHelper
    {
        CMemCached mc = new CMemCached();

        /// <summary>
        /// 获取所有的材料清单数据
        /// </summary>
        /// <param name="pageSize">页面记录数</param>
        /// <param name="currentPage">当前页</param>
        /// <param name="pager">分页控件</param>
        /// <param name="sortExpression">排序字段</param>
        /// <param name="sortDirection">排序方式</param>
        /// <param name="condition">查询条件</param>
        /// <returns></returns>
        public DataTable GetAllMaterailList(int pageSize, int currentPage, Wuqi.Webdiyer.AspNetPager pager, string sortExpression, string sortDirection, string condition)
        {
            int totalPage = 0, totalRow = 0;//总页数，总记录数
            SQLScript sqlScript = new SQLScript();
            DataTable table = null;

            try
            {
                if (mc.KeyExist("materialList" + currentPage))
                {
                    //获取
                    table = (DataTable)mc.GetObject("materialList" + currentPage);
                    totalRow = (int)mc.GetObject("materialListTotalRow");
                }
                else
                {
                    IDBAccess DB = DBFactory.GetDBAccess(OConfig.SysDBType, OConfig.DBConnection);
                    int startIndex, endIndex;
                    string strCol = " a.*";
                    OConfig.GetStartRowNum(" tb_company_material_list a ", condition, currentPage, pageSize, out startIndex, out endIndex, out totalPage, out totalRow);
                    string sqlCommand = sqlScript.QueryByDividePage(" tb_company_material_list a ", condition, strCol, sortExpression + " " + sortDirection, startIndex, endIndex);
                    table = DB.ExecuteDataTable(sqlCommand, CommandType.Text, null);

                    //存储
                    mc.SetObject("materialList" + currentPage, table);
                    mc.SetObject("materialListTotalRow", totalRow);
                }

                pager.RecordCount = totalRow;
                if (currentPage > pager.PageCount)
                {
                    currentPage = pager.PageCount;
                }
                if (currentPage == 0)
                {
                    currentPage = 1;
                }
                pager.CurrentPageIndex = currentPage;
            }
            catch (Exception exp)
            {
                table = null;
            }
            finally
            {
                pager.CustomInfoHTML = "共<font color=red>" + pager.RecordCount + "</font>条数据 当前<font color=red>" + pager.CurrentPageIndex + "/" + pager.PageCount + "</font>页";
            }
            //mc.ClosePool();
            return table;

        }


        /// <summary>
        /// 添加一条记录
        /// </summary>
        /// <param name="modelInfo">材料清单实体类</param>
        /// <returns></returns>
        public bool AddRow(MaterialListInfo model)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                StringBuilder strSql1 = new StringBuilder();
                StringBuilder strSql2 = new StringBuilder();
                DBType dbType = OConfig.SysDBType;
                IDBAccess DB = DBFactory.GetDBAccess(dbType, OConfig.DBConnection);
                if (model.ListGuid != null)
                {
                    strSql1.Append("listGuid,");
                    strSql2.Append("'" + model.ListGuid + "',");
                }
                if (model.CompanyType != null)
                {
                    strSql1.Append("CompanyType,");
                    strSql2.Append("'" + model.CompanyType + "',");
                }
                if (model.MaterialType != null)
                {
                    strSql1.Append("MaterialType,");
                    strSql2.Append("'" + model.MaterialType + "',");
                }
                if (model.MaterialContent != null)
                {
                    strSql1.Append("MaterialContent,");
                    strSql2.Append("'" + model.MaterialContent + "',");
                }
                if (model.MaterialFile != null)
                {
                    strSql1.Append("MaterialFile,");
                    strSql2.Append("'" + model.MaterialFile + "',");
                }
                if (model.MaterialFilePath != null)
                {
                    strSql1.Append("MaterialFilePath,");
                    strSql2.Append("'" + model.MaterialFilePath + "',");
                }

                strSql1.Append("IsHaveFile,");
                strSql2.Append("'" + model.IsHaveFile.ToString() + "',");



                strSql.Append("insert into tb_company_material_list(");
                strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
                strSql.Append(")");
                strSql.Append(" values (");
                strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
                strSql.Append(")");
                int isSuccess = DB.ExecuteNonQuery(strSql.ToString(), CommandType.Text, null);
                if (isSuccess > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                return false;
            }

        }
        /// <summary>
        /// 编辑一条记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool EditRow(MaterialListInfo model)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                DBType dbType = OConfig.SysDBType;
                IDBAccess DB = DBFactory.GetDBAccess(dbType, OConfig.DBConnection);
                strSql.Append("update tb_company_material_list set ");
                if (model.ListGuid != null)
                {
                    strSql.Append("listGuid='" + model.ListGuid + "',");
                }
                if (model.CompanyType != null)
                {
                    strSql.Append("CompanyType='" + model.CompanyType + "',");
                }
                if (model.MaterialType != null)
                {
                    strSql.Append("MaterialType='" + model.MaterialType + "',");
                }
                if (model.MaterialContent != null)
                {
                    strSql.Append("MaterialContent='" + model.MaterialContent + "',");
                }
                if (model.MaterialFile != null)
                {
                    strSql.Append("MaterialFile='" + model.MaterialFile + "',");
                }
                if (model.MaterialFilePath != null)
                {
                    strSql.Append("MaterialFilePath='" + model.MaterialFilePath + "',");
                }
                if (model.IsHaveFile != null)
                {
                    strSql.Append("IsHaveFile='" + model.IsHaveFile + "',");
                }
                int n = strSql.ToString().LastIndexOf(",");
                strSql.Remove(n, 1);
                strSql.Append(" where listGuid='" + model.ListGuid + "' ");
                int isSuccess = DB.ExecuteNonQuery(strSql.ToString(), CommandType.Text, null);
                if (isSuccess > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                return false;
            }

        }
        /// <summary>
        /// 删除记录
        /// </summary>
        /// <param name="listGuid">主键ID</param>
        /// <returns></returns>
        public bool DelRow(String listGuid)
        {
            StringBuilder strSql = new StringBuilder();
            DBType dbType = OConfig.SysDBType;
            IDBAccess DB = DBFactory.GetDBAccess(dbType, OConfig.DBConnection);
            strSql.Append("delete from tb_company_material_list ");
            strSql.Append(" where listGuid='" + listGuid + "' ");
            int isSuccess = DB.ExecuteNonQuery(strSql.ToString(), CommandType.Text, null);
            if (isSuccess > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public MaterialListInfo GetMaterial(string listGUID)
        {
            StringBuilder strSql = new StringBuilder();
            DBType dbType = OConfig.SysDBType;
            IDBAccess DB = DBFactory.GetDBAccess(dbType, OConfig.DBConnection);
            strSql.Append("select  top 1  ");
            strSql.Append(" * ");
            strSql.Append(" from tb_company_material_list ");
            strSql.Append(" where listGuid='" + listGUID + "' ");
            MaterialListInfo model = new MaterialListInfo();
            DataSet ds = DB.ExecuteDataSet(strSql.ToString(), CommandType.Text, null);
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.ListGuid = ds.Tables[0].Rows[0]["listGuid"].ToString();
                model.CompanyType = ds.Tables[0].Rows[0]["CompanyType"].ToString();
                model.MaterialType = ds.Tables[0].Rows[0]["MaterialType"].ToString();
                model.MaterialContent = ds.Tables[0].Rows[0]["MaterialContent"].ToString();
                model.MaterialFile = ds.Tables[0].Rows[0]["MaterialFile"].ToString();
                model.MaterialFilePath = ds.Tables[0].Rows[0]["MaterialFilePath"].ToString();
                if (ds.Tables[0].Rows[0]["IsHaveFile"] != null && ds.Tables[0].Rows[0]["IsHaveFile"].ToString().Length > 0)
                {
                   // model.IsHaveFile = Convert.ToBoolean(ds.Tables[0].Rows[0]["IsHaveFile"].ToString());
                }

                return model;
            }
            else
            {
                return null;
            }
        }

       
        
    }
}
